package com.DosonLeung.homeschoolcommunication.SQLite;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

/**
 * Created by DosonLeung on 2014/11/6.
 */
public class ChatsDB extends SQLiteOpenHelper
{
    private String target;
    //private SQLiteDatabase DB;
    /**
     * @param context
     * @param name
     * @param version
     */


    public ChatsDB(Context context, String name, String target, int version)
    {
        super(context, name, null, version);
        String tablePrefix = "JXT";
        this.target = tablePrefix + target;
        //DB = getReadableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase DB)
    {
        try {
            String SQL = "create table " + target + "(uid VARCHAR(128), time VARCHAR(128) primary key, " +
                    "contentType VARCHAR(128), " +
                    " content TEXT, url VARCHAR(128), length VARCHAR(8), contentLevel VARCHAR(64), extra VARCHAR(128))";
            DB.execSQL(SQL);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        onCreate(db);
    }

    public void insertChattingData(HashMap<String, String> item){
        try {
            String uid = item.get("uid");
            String time = item.get("time");
            String contentType = item.get("contentType");
            String content = item.get("content");
            String url = item.get("url");
            String length = item.get("length");
            String contentLevel = item.get("contentLevel");
            String extra = item.get("extra");
            String SQL = String.format("insert into " + target + " " +
                    "(uid,time,contentType,content,url,length,contentLevel,extra) "+
                    "values('%s' , '%s' , '%s' , '%s' , '%s' , '%s' , '%s' , '%s')",
                    uid,time,contentType,content,url,length,contentLevel==null?"":contentLevel,extra==null?"":extra);
            this.getReadableDatabase().execSQL(SQL);
        }catch (Exception e){
            e.printStackTrace();
        }
        //getChattingData();
    }

    //从数据库获取出聊天记录（sum : 聊天记录数量）
    public LinkedList<Map<String, String>> getChattingData(){
        LinkedList<Map<String,String>> list = new LinkedList<Map<String, String>>();
        try {
            Cursor cursor = this.getReadableDatabase().rawQuery("select * from " + target, null);
            while (cursor.moveToNext()) {
                //将结果集中的数据存入ArrayList中
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("uid", cursor.getString(0));
                map.put("time", cursor.getString(1));
                map.put("contentType", cursor.getString(2));
                map.put("content", cursor.getString(3));
                map.put("url", cursor.getString(4));
                map.put("length", cursor.getString(5));
                map.put("contentLevel",cursor.getString(6));
                map.put("extra",cursor.getString(7));
                list.add(map);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    //从数据库获取出聊天记录（sum : 聊天记录数量）
    public LinkedList<Map<String, String>> getChattingData(int start,int end){
        LinkedList<Map<String,String>> list = new LinkedList<Map<String, String>>();
        try {
            String offset = (Integer.valueOf(start)).toString();
            String limit = (Integer.valueOf(end-start)).toString();
            Cursor cursor = this.getReadableDatabase().rawQuery("select * from " + target + " order by time desc" +
                    " limit " + limit + " offset " + offset, null);
            while (cursor.moveToNext()) {
                //将结果集中的数据存入ArrayList中
                HashMap<String, String> map = new HashMap<String, String>();
                map.put("uid", cursor.getString(0));
                map.put("time", cursor.getString(1));
                map.put("contentType", cursor.getString(2));
                map.put("content", cursor.getString(3));
                map.put("url", cursor.getString(4));
                map.put("length", cursor.getString(5));
                map.put("contentLevel",cursor.getString(6));
                map.put("extra",cursor.getString(7));
                list.add(map);
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;
    }

    public void updateChattingData(HashMap<String, String> item){
        try {
            String uid = item.get("uid");
            String time = item.get("time");
            String contentType = item.get("contentType");
            String content = item.get("content");
            String url = item.get("url");
            String length = item.get("length");
            String contentLevel = item.get("level");
            String extra = item.get("extra");
            String SQL = String.format("update " + target + " " +"set "+
                    "uid = '%s',"+"contentType = '%s',"+"content = '%s',"+"url = '%s',"+"length = %s"
                    +"contentLevel = '%s',"+"extra = '%s' "+
                    "where time = '%s'",uid,contentType,content,url,length,contentLevel==null?"":contentLevel,
                    extra==null?"":extra,time);
            this.getReadableDatabase().execSQL(SQL);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //清空聊天目标的聊天记录
    public void cleanTable(){
        this.getReadableDatabase().execSQL("delete from "+target);
    }

    public int getCount(){
        String sum = "0";
        try {
            Cursor cursor = this.getReadableDatabase().rawQuery("select count(time) from " + target, null);
            while (cursor.moveToNext()) {
                sum = cursor.getString(0);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Integer.parseInt(sum);
    }
}
